home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 3 / CD ACTUAL 3.iso / linux / incoming / libgr-2.000 / libgr-2 / libgr-2.0.3 / tiff / tif_write.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-20  |  17.8 KB  |  593 lines

  1. /* /usr/local/src/CVS/libgr/tiff/tif_write.c,v 1.2 1995/08/20 12:51:37 neal Exp */
  2.  
  3. /*
  4.  * Copyright (c) 1988-1995 Sam Leffler
  5.  * Copyright (c) 1991-1995 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26.  
  27. /*
  28.  * TIFF Library.
  29.  *
  30.  * Scanline-oriented Write Support
  31.  */
  32. #include "tiffiop.h"
  33. #include <assert.h>
  34. #include <stdio.h>
  35.  
  36. #define    STRIPINCR    20        /* expansion factor on strip array */
  37.  
  38. #define    WRITECHECKSTRIPS(tif, module)                \
  39.     (((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),0,module))
  40. #define    WRITECHECKTILES(tif, module)                \
  41.     (((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),1,module))
  42. #define    BUFFERCHECK(tif)                    \
  43.     (((tif)->tif_flags & TIFF_BUFFERSETUP) ||        \
  44.         TIFFWriteBufferSetup((tif), NULL, (tsize_t) -1))
  45.  
  46. static    int TIFFWriteCheck(TIFF*, int, const char*);
  47. static    int TIFFGrowStrips(TIFF*, int, const char*);
  48. static    int TIFFAppendToStrip(TIFF*, tstrip_t, tidata_t, tsize_t);
  49. static    int TIFFSetupStrips(TIFF*);
  50.  
  51. int
  52. TIFFWriteScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
  53. {
  54.     static const char module[] = "TIFFWriteScanline";
  55.     register TIFFDirectory *td;
  56.     int status, imagegrew = 0;
  57.     tstrip_t strip;
  58.  
  59.     if (!WRITECHECKSTRIPS(tif, module))
  60.         return (-1);
  61.     /*
  62.      * Handle delayed allocation of data buffer.  This
  63.      * permits it to be sized more intelligently (using
  64.      * directory information).
  65.      */
  66.     if (!BUFFERCHECK(tif))
  67.         return (-1);
  68.     td = &tif->tif_dir;
  69.     /*
  70.      * Extend image length if needed
  71.      * (but only for PlanarConfig=1).
  72.      */
  73.     if (row >= td->td_imagelength) {    /* extend image */
  74.         if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  75.             TIFFError(tif->tif_name,
  76.         "Can not change \"ImageLength\" when using separate planes");
  77.             return (-1);
  78.         }
  79.         td->td_imagelength = row+1;
  80.         imagegrew = 1;
  81.     }
  82.     /*
  83.      * Calculate strip and check for crossings.
  84.      */
  85.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  86.         if (sample >= td->td_samplesperpixel) {
  87.             TIFFError(tif->tif_name,
  88.                 "%d: Sample out of range, max %d",
  89.                 sample, td->td_samplesperpixel);
  90.             return (-1);
  91.         }
  92.         strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
  93.     } else
  94.         strip = row / td->td_rowsperstrip;
  95.     if (strip != tif->tif_curstrip) {
  96.         /*
  97.          * Changing strips -- flush any data present.
  98.          */
  99.         if (!TIFFFlushData(tif))
  100.             return (-1);
  101.         tif->tif_curstrip = strip;
  102.         /*
  103.          * Watch out for a growing image.  The value of
  104.          * strips/image will initially be 1 (since it
  105.          * can't be deduced until the imagelength is known).
  106.          */
  107.         if (strip >= td->td_stripsperimage && imagegrew)
  108.             td->td_stripsperimage =
  109.                 TIFFhowmany(td->td_imagelength,td->td_rowsperstrip);
  110.         tif->tif_row =
  111.             (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  112.         if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  113.             if (!(*tif->tif_setupencode)(tif))
  114.                 return (-1);
  115.             tif->tif_flags |= TIFF_CODERSETUP;
  116.         }
  117.         if (!(*tif->tif_preencode)(tif, sample))
  118.             return (-1);
  119.         tif->tif_flags |= TIFF_POSTENCODE;
  120.     }
  121.     /*
  122.      * Check strip array to make sure there's space.
  123.      * We don't support dynamically growing files that
  124.      * have data organized in separate bitplanes because
  125.      * it's too painful.  In that case we require that
  126.      * the imagelength be set properly before the first
  127.      * write (so that the strips array will be fully
  128.      * allocated above).
  129.      */
  130.     if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module))
  131.         return (-1);
  132.     /*
  133.      * Ensure the write is either sequential or at the
  134.      * beginning of a strip (or that we can randomly
  135.      * access the data -- i.e. no encoding).
  136.      */
  137.     if (row != tif->tif_row) {
  138.         if (row < tif->tif_row) {
  139.             /*
  140.              * Moving backwards within the same strip:
  141.              * backup to the start and then decode
  142.              * forward (below).
  143.              */
  144.             tif->tif_row = (strip % td->td_stripsperimage) *
  145.                 td->td_rowsperstrip;
  146.             tif->tif_rawcp = tif->tif_rawdata;
  147.         }
  148.         /*
  149.          * Seek forward to the desired row.
  150.          */
  151.         if (!(*tif->tif_seek)(tif, row - tif->tif_row))
  152.             return (-1);
  153.         tif->tif_row = row;
  154.     }
  155.     status = (*tif->tif_encoderow)(tif, (tidata_t) buf,
  156.         tif->tif_scanlinesize, sample);
  157.     tif->tif_row++;
  158.     return (status);
  159. }
  160.  
  161. /*
  162.  * Encode the supplied data and write it to the
  163.  * specified strip.  There must be space for the
  164.  * data; we don't check if strips overlap!
  165.  *
  166.  * NB: Image length must be setup before writing; this
  167.  *     interface does not support automatically growing
  168.  *     the image on each write (as TIFFWriteScanline does).
  169.  */
  170. tsize_t
  171. TIFFWriteEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t data, tsize_t cc)
  172. {
  173.     static const char module[] = "TIFFWriteEncodedStrip";
  174.     TIFFDirectory *td = &tif->tif_dir;
  175.     tsample_t sample;
  176.  
  177.     if (!WRITECHECKSTRIPS(tif, module))
  178.         return ((tsize_t) -1);
  179.     if (strip >= td->td_nstrips) {
  180.         TIFFError(module, "%s: Strip %lu out of range, max %lu",
  181.             tif->tif_name, (u_long) strip, (u_long) td->td_nstrips);
  182.         return ((tsize_t) -1);
  183.     }
  184.     /*
  185.      * Handle delayed allocation of data buffer.  This
  186.      * permits it to be sized according to the directory
  187.      * info.
  188.      */
  189.     if (!BUFFERCHECK(tif))
  190.         return ((tsize_t) -1);
  191.     tif->tif_curstrip = strip;
  192.     tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  193.     if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  194.         if (!(*tif->tif_setupencode)(tif))
  195.             return ((tsize_t) -1);
  196.         tif->tif_flags |= TIFF_CODERSETUP;
  197.     }
  198.     tif->tif_flags &= ~TIFF_POSTENCODE;
  199.     sample = (tsample_t)(strip / td->td_stripsperimage);
  200.     if (!(*tif->tif_preencode)(tif, sample))
  201.         return ((tsize_t) -1);
  202.     if (!(*tif->tif_encodestrip)(tif, (tidata_t) data, cc, sample))
  203.         return ((tsize_t) 0);
  204.     if (!(*tif->tif_postencode)(tif))
  205.         return (-1);
  206.     if (!isFillOrder(tif, td->td_fillorder) &&
  207.         (tif->tif_flags & TIFF_NOBITREV) == 0)
  208.         TIFFReverseBits(tif->tif_rawdata, tif->tif_rawcc);
  209.     if (tif->tif_rawcc > 0 &&
  210.         !TIFFAppendToStrip(tif, strip, tif->tif_rawdata, tif->tif_rawcc))
  211.         return (-1);
  212.     tif->tif_rawcc = 0;
  213.     tif->tif_rawcp = tif->tif_rawdata;
  214.     return (cc);
  215. }
  216.  
  217. /*
  218.  * Write the supplied data to the specified strip.
  219.  * There must be space for the data; we don't check
  220.  * if strips overlap!
  221.  *
  222.  * NB: Image length must be setup before writing; this
  223.  *     interface does not support automatically growing
  224.  *     the image on each write (as TIFFWriteScanline does).
  225.  */
  226. tsize_t
  227. TIFFWriteRawStrip(TIFF* tif, tstrip_t strip, tdata_t data, tsize_t cc)
  228. {
  229.     static const char module[] = "TIFFWriteRawStrip";
  230.  
  231.     if (!WRITECHECKSTRIPS(tif, module))
  232.         return ((tsize_t) -1);
  233.     if (strip >= tif->tif_dir.td_nstrips) {
  234.         TIFFError(module, "%s: Strip %lu out of range, max %lu",
  235.             tif->tif_name, (u_long) strip,
  236.             (u_long) tif->tif_dir.td_nstrips);
  237.         return ((tsize_t) -1);
  238.     }
  239.     return (TIFFAppendToStrip(tif, strip, (tidata_t) data, cc) ?
  240.         cc : (tsize_t) -1);
  241. }
  242.  
  243. /*
  244.  * Write and compress a tile of data.  The
  245.  * tile is selected by the (x,y,z,s) coordinates.
  246.  */
  247. tsize_t
  248. TIFFWriteTile(TIFF* tif,
  249.     tdata_t buf, uint32 x, uint32 y, uint32 z, tsample_t s)
  250. {
  251.     if (!TIFFCheckTile(tif, x, y, z, s))
  252.         return (-1);
  253.     /*
  254.      * NB: A tile size of -1 is used instead of tif_tilesize knowing
  255.      *     that TIFFWriteEncodedTile will clamp this to the tile size.
  256.      *     This is done because the tile size may not be defined until
  257.      *     after the output buffer is setup in TIFFWriteBufferSetup.
  258.      */
  259.     return (TIFFWriteEncodedTile(tif,
  260.         TIFFComputeTile(tif, x, y, z, s), buf, (tsize_t) -1));
  261. }
  262.  
  263. /*
  264.  * Encode the supplied data and write it to the
  265.  * specified tile.  There must be space for the
  266.  * data.  The function clamps individual writes
  267.  * to a tile to the tile size, but does not (and
  268.  * can not) check that multiple writes to the same
  269.  * tile do not write more than tile size data.
  270.  *
  271.  * NB: Image length must be setup before writing; this
  272.  *     interface does not support automatically growing
  273.  *     the image on each write (as TIFFWriteScanline does).
  274.  */
  275. tsize_t
  276. TIFFWriteEncodedTile(TIFF* tif, ttile_t tile, tdata_t data, tsize_t cc)
  277. {
  278.     static const char module[] = "TIFFWriteEncodedTile";
  279.     TIFFDirectory *td;
  280.     tsample_t sample;
  281.  
  282.     if (!WRITECHECKTILES(tif, module))
  283.         return ((tsize_t) -1);
  284.     td = &tif->tif_dir;
  285.     if (tile >= td->td_nstrips) {
  286.         TIFFError(module, "%s: Tile %lu out of range, max %lu",
  287.             tif->tif_name, (u_long) tile, (u_long) td->td_nstrips);
  288.         return ((tsize_t) -1);
  289.     }
  290.     /*
  291.      * Handle delayed allocation of data buffer.  This
  292.      * permits it to be sized more intelligently (using
  293.      * directory information).
  294.      */
  295.     if (!BUFFERCHECK(tif))
  296.         return ((tsize_t) -1);
  297.     tif->tif_curtile = tile;
  298.     /* 
  299.      * Compute tiles per row & per column to compute
  300.      * current row and column
  301.      */
  302.     tif->tif_row = (tile % TIFFhowmany(td->td_imagelength, td->td_tilelength))
  303.         * td->td_tilelength;
  304.     tif->tif_col = (tile % TIFFhowmany(td->td_imagewidth, td->td_tilewidth))
  305.         * td->td_tilewidth;
  306.  
  307.     if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  308.         if (!(*tif->tif_setupencode)(tif))
  309.             return ((tsize_t) -1);
  310.         tif->tif_flags |= TIFF_CODERSETUP;
  311.     }
  312.     tif->tif_flags &= ~TIFF_POSTENCODE;
  313.     sample = (tsample_t)(tile/td->td_stripsperimage);
  314.     if (!(*tif->tif_preencode)(tif, sample))
  315.         return ((tsize_t) -1);
  316.     /*
  317.      * Clamp write amount to the tile size.  This is mostly
  318.      * done so that callers can pass in some large number
  319.      * (e.g. -1) and have the tile size used instead.
  320.      */
  321.     if ((uint32) cc > tif->tif_tilesize)
  322.         cc = tif->tif_tilesize;
  323.     if (!(*tif->tif_encodetile)(tif, (tidata_t) data, cc, sample))
  324.         return ((tsize_t) 0);
  325.     if (!(*tif->tif_postencode)(tif))
  326.         return ((tsize_t) -1);
  327.     if (!isFillOrder(tif, td->td_fillorder) &&
  328.         (tif->tif_flags & TIFF_NOBITREV) == 0)
  329.         TIFFReverseBits((u_char *)tif->tif_rawdata, tif->tif_rawcc);
  330.     if (tif->tif_rawcc > 0 && !TIFFAppendToStrip(tif, tile,
  331.         tif->tif_rawdata, tif->tif_rawcc))
  332.         return ((tsize_t) -1);
  333.     tif->tif_rawcc = 0;
  334.     tif->tif_rawcp = tif->tif_rawdata;
  335.     return (cc);
  336. }
  337.  
  338. /*
  339.  * Write the supplied data to the specified strip.
  340.  * There must be space for the data; we don't check
  341.  * if strips overlap!
  342.  *
  343.  * NB: Image length must be setup before writing; this
  344.  *     interface does not support automatically growing
  345.  *     the image on each write (as TIFFWriteScanline does).
  346.  */
  347. tsize_t
  348. TIFFWriteRawTile(TIFF* tif, ttile_t tile, tdata_t data, tsize_t cc)
  349. {
  350.     static const char module[] = "TIFFWriteRawTile";
  351.  
  352.     if (!WRITECHECKTILES(tif, module))
  353.         return ((tsize_t) -1);
  354.     if (tile >= tif->tif_dir.td_nstrips) {
  355.         TIFFError(module, "%s: Tile %lu out of range, max %lu",
  356.             tif->tif_name, (u_long) tile,
  357.             (u_long) tif->tif_dir.td_nstrips);
  358.         return ((tsize_t) -1);
  359.     }
  360.     return (TIFFAppendToStrip(tif, tile, (tidata_t) data, cc) ?
  361.         cc : (tsize_t) -1);
  362. }
  363.  
  364. #define    isUnspecified(td, v) \
  365.     (td->v == (uint32) -1 || (td)->td_imagelength == 0)
  366.  
  367. static int
  368. TIFFSetupStrips(TIFF* tif)
  369. {
  370.     TIFFDirectory* td = &tif->tif_dir;
  371.  
  372.     if (isTiled(tif))
  373.         td->td_stripsperimage = isUnspecified(td, td_tilelength) ?
  374.             td->td_samplesperpixel : TIFFNumberOfTiles(tif);
  375.     else
  376.         td->td_stripsperimage = isUnspecified(td, td_rowsperstrip) ?
  377.             td->td_samplesperpixel : TIFFNumberOfStrips(tif);
  378.     td->td_nstrips = td->td_stripsperimage;
  379.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  380.         td->td_stripsperimage /= td->td_samplesperpixel;
  381.     td->td_stripoffset = (uint32 *)
  382.         _TIFFmalloc(td->td_nstrips * sizeof (uint32));
  383.     td->td_stripbytecount = (uint32 *)
  384.         _TIFFmalloc(td->td_nstrips * sizeof (uint32));
  385.     if (td->td_stripoffset == NULL || td->td_stripbytecount == NULL)
  386.         return (0);
  387.     /*
  388.      * Place data at the end-of-file
  389.      * (by setting offsets to zero).
  390.      */
  391.     _TIFFmemset(td->td_stripoffset, 0, td->td_nstrips*sizeof (uint32));
  392.     _TIFFmemset(td->td_stripbytecount, 0, td->td_nstrips*sizeof (uint32));
  393.     TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
  394.     TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
  395.     return (1);
  396. }
  397. #undef isUnspecified
  398.  
  399. /*
  400.  * Verify file is writable and that the directory
  401.  * information is setup properly.  In doing the latter
  402.  * we also "freeze" the state of the directory so
  403.  * that important information is not changed.
  404.  */
  405. static int
  406. TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
  407. {
  408.     if (tif->tif_mode == O_RDONLY) {
  409.         TIFFError(module, "%s: File not open for writing",
  410.             tif->tif_name);
  411.         return (0);
  412.     }
  413.     if (tiles ^ isTiled(tif)) {
  414.         TIFFError(tif->tif_name, tiles ?
  415.             "Can not write tiles to a stripped image" :
  416.             "Can not write scanlines to a tiled image");
  417.         return (0);
  418.     }
  419.     /*
  420.      * On the first write verify all the required information
  421.      * has been setup and initialize any data structures that
  422.      * had to wait until directory information was set.
  423.      * Note that a lot of our work is assumed to remain valid
  424.      * because we disallow any of the important parameters
  425.      * from changing after we start writing (i.e. once
  426.      * TIFF_BEENWRITING is set, TIFFSetField will only allow
  427.      * the image's length to be changed).
  428.      */
  429.     if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
  430.         TIFFError(module,
  431.             "%s: Must set \"ImageWidth\" before writing data",
  432.             tif->tif_name);
  433.         return (0);
  434.     }
  435.     if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
  436.         TIFFError(module,
  437.         "%s: Must set \"PlanarConfiguration\" before writing data",
  438.             tif->tif_name);
  439.         return (0);
  440.     }
  441.     if (tif->tif_dir.td_stripoffset == NULL && !TIFFSetupStrips(tif)) {
  442.         tif->tif_dir.td_nstrips = 0;
  443.         TIFFError(module, "%s: No space for %s arrays",
  444.             tif->tif_name, isTiled(tif) ? "tile" : "strip");
  445.         return (0);
  446.     }
  447.     tif->tif_tilesize = TIFFTileSize(tif);
  448.     tif->tif_scanlinesize = TIFFScanlineSize(tif);
  449.     tif->tif_flags |= TIFF_BEENWRITING;
  450.     return (1);
  451. }
  452.  
  453. /*
  454.  * Setup the raw data buffer used for encoding.
  455.  */
  456. int
  457. TIFFWriteBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
  458. {
  459.     static const char module[] = "TIFFWriteBufferSetup";
  460.  
  461.     if (tif->tif_rawdata) {
  462.         if (tif->tif_flags & TIFF_MYBUFFER) {
  463.             _TIFFfree(tif->tif_rawdata);
  464.             tif->tif_flags &= ~TIFF_MYBUFFER;
  465.         }
  466.         tif->tif_rawdata = NULL;
  467.     }
  468.     if (size == (tsize_t) -1) {
  469.         size = (isTiled(tif) ?
  470.             tif->tif_tilesize : tif->tif_scanlinesize);
  471.         /*
  472.          * Make raw data buffer at least 8K
  473.          */
  474.         if (size < 8*1024)
  475.             size = 8*1024;
  476.         bp = NULL;            /* NB: force malloc */
  477.     }
  478.     if (bp == NULL) {
  479.         bp = _TIFFmalloc(size);
  480.         if (bp == NULL) {
  481.             TIFFError(module, "%s: No space for output buffer",
  482.                 tif->tif_name);
  483.             return (0);
  484.         }
  485.         tif->tif_flags |= TIFF_MYBUFFER;
  486.     } else
  487.         tif->tif_flags &= ~TIFF_MYBUFFER;
  488.     tif->tif_rawdata = (tidata_t) bp;
  489.     tif->tif_rawdatasize = size;
  490.     tif->tif_rawcc = 0;
  491.     tif->tif_rawcp = tif->tif_rawdata;
  492.     tif->tif_flags |= TIFF_BUFFERSETUP;
  493.     return (1);
  494. }
  495.  
  496. /*
  497.  * Grow the strip data structures by delta strips.
  498.  */
  499. static int
  500. TIFFGrowStrips(TIFF* tif, int delta, const char* module)
  501. {
  502.     TIFFDirectory *td = &tif->tif_dir;
  503.  
  504.     assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
  505.     td->td_stripoffset = (uint32*)_TIFFrealloc(td->td_stripoffset,
  506.         (td->td_nstrips + delta) * sizeof (uint32));
  507.     td->td_stripbytecount = (uint32*)_TIFFrealloc(td->td_stripbytecount,
  508.         (td->td_nstrips + delta) * sizeof (uint32));
  509.     if (td->td_stripoffset == NULL || td->td_stripbytecount == NULL) {
  510.         td->td_nstrips = 0;
  511.         TIFFError(module, "%s: No space to expand strip arrays",
  512.             tif->tif_name);
  513.         return (0);
  514.     }
  515.     _TIFFmemset(td->td_stripoffset+td->td_nstrips, 0, delta*sizeof (uint32));
  516.     _TIFFmemset(td->td_stripbytecount+td->td_nstrips, 0, delta*sizeof (uint32));
  517.     td->td_nstrips += delta;
  518.     return (1);
  519. }
  520.  
  521. /*
  522.  * Append the data to the specified strip.
  523.  *
  524.  * NB: We don't check that there's space in the
  525.  *     file (i.e. that strips do not overlap).
  526.  */
  527. static int
  528. TIFFAppendToStrip(TIFF* tif, tstrip_t strip, tidata_t data, tsize_t cc)
  529. {
  530.     TIFFDirectory *td = &tif->tif_dir;
  531.     static const char module[] = "TIFFAppendToStrip";
  532.  
  533.     if (td->td_stripoffset[strip] == 0 || tif->tif_curoff == 0) {
  534.         /*
  535.          * No current offset, set the current strip.
  536.          */
  537.         if (td->td_stripoffset[strip] != 0) {
  538.             if (!SeekOK(tif, td->td_stripoffset[strip])) {
  539.                 TIFFError(module,
  540.                     "%s: Seek error at scanline %lu",
  541.                     tif->tif_name, (u_long) tif->tif_row);
  542.                 return (0);
  543.             }
  544.         } else
  545.             td->td_stripoffset[strip] =
  546.                 TIFFSeekFile(tif, (toff_t) 0, SEEK_END);
  547.         tif->tif_curoff = td->td_stripoffset[strip];
  548.     }
  549.     if (!WriteOK(tif, data, cc)) {
  550.         TIFFError(module, "%s: Write error at scanline %lu",
  551.             tif->tif_name, (u_long) tif->tif_row);
  552.         return (0);
  553.     }
  554.     tif->tif_curoff += cc;
  555.     td->td_stripbytecount[strip] += cc;
  556.     return (1);
  557. }
  558.  
  559. /*
  560.  * Internal version of TIFFFlushData that can be
  561.  * called by ``encodestrip routines'' w/o concern
  562.  * for infinite recursion.
  563.  */
  564. int
  565. TIFFFlushData1(TIFF* tif)
  566. {
  567.     if (tif->tif_rawcc > 0) {
  568.         if (!isFillOrder(tif, tif->tif_dir.td_fillorder) &&
  569.             (tif->tif_flags & TIFF_NOBITREV) == 0)
  570.             TIFFReverseBits((u_char *)tif->tif_rawdata,
  571.                 tif->tif_rawcc);
  572.         if (!TIFFAppendToStrip(tif,
  573.             isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip,
  574.             tif->tif_rawdata, tif->tif_rawcc))
  575.             return (0);
  576.         tif->tif_rawcc = 0;
  577.         tif->tif_rawcp = tif->tif_rawdata;
  578.     }
  579.     return (1);
  580. }
  581.  
  582. /*
  583.  * Set the current write offset.  This should only be
  584.  * used to set the offset to a known previous location
  585.  * (very carefully), or to 0 so that the next write gets
  586.  * appended to the end of the file.
  587.  */
  588. void
  589. TIFFSetWriteOffset(TIFF* tif, toff_t off)
  590. {
  591.     tif->tif_curoff = off;
  592. }
  593.